package com.vanysoft.business.demo.form.controller;

import com.vanysoft.business.demo.form.controller.demo.DemoFormObj;
import com.vanysoft.business.demo.form.entity.TDemoForm;
import com.vanysoft.business.demo.form.service.DemoFormService;
import com.vanysoft.business.sys.entity.Org;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.system.systemLog.entity.TSystemLog;
import com.vanysoft.business.system.systemLog.service.SystemLogService;
import com.vanysoft.common.utils.date.DateUtil;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.UUID;

/**
 * 类名称：DemoFormAction
 * 类描述：DEMO表单控制层类
 * 创建人：SharpClaw_X
 * 创建时间：2017年10月21日下午7:31:11
 * 修改人：SharpClaw_X
 * 修改时间：2017年10月21日
 * 版本信息：@version 1.0
 * Copyright (c) 2017 SharpClaw
 * 修改备注：
 */
@RestController
@RequestMapping("/demo/demoForm")
public class DemoFormAction extends BaseController {


    @Autowired
    private DemoFormService demoFormService;
    @Autowired
    private SystemLogService systemLogService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @VanySoftValidation
    public JSONResponse addDemoForm(@SessionUser UserInfo userInfo, HttpServletRequest request, @Valid DemoFormObj demoForm, BindingResult result) throws VanySoftBaseException {
        TDemoForm form = new TDemoForm();
        if (demoForm.isDelivery()) {
            form.setDelivery("1");
        } else {
            form.setDelivery("0");
        }
        form.setType(demoForm.getType());
        form.setName(demoForm.getName());
        if (demoForm.getDemoDate() != null && !"".equalsIgnoreCase(demoForm.getDemoDate())) {
            Date demoDate = DateUtil.string2Date(demoForm.getDemoDate(), "yyyy-MM-dd");
            form.setDemodate(demoDate);
        }

        form.setIndustry(demoForm.getIndustry());
        form.setRemark(demoForm.getRemark());
        form.setResource(demoForm.getResource());
        form.setUseraccount(userInfo.getUsername());
        form.setOprdate(new Date());
        //业务日志存储
        TSystemLog systemLog = new TSystemLog();
        String logid = UUID.randomUUID().toString().replaceAll("-", "");
        systemLog.setLogid(logid);
        systemLog.setUserid(userInfo.getId());
        systemLog.setUsername(userInfo.getChineseName());
        systemLog.setLogip(request.getRemoteAddr());
        systemLog.setOperationtime(new Date());
        systemLog.setOperationcontent("DEMO表单数据新增，新增信息为：" + form.toString());
        if (userInfo.getOrgs().size() > 0) {
            Org org = userInfo.getOrgs().get(0);
            systemLog.setOrgcode(org.getCode());
            systemLog.setOrgname(org.getOrgName());
        }
        systemLog.setOperationtype("DEMO_TABLE_ADD");
        systemLog.setOperationname("DEMO表单_新增");
        try {
            demoFormService.addTDemoForm(form);
            systemLog.setStatus("SUCCESS");
            systemLogService.insertSystemLogs(systemLog);
        } catch (Exception e) {
            e.printStackTrace();
            systemLog.setStatus("FAIL");
            systemLogService.insertSystemLogs(systemLog);
        }
        return new JSONResponse();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @VanySoftValidation
    public JSONResponse updateDemoForm(HttpServletRequest request, @SessionUser UserInfo userInfo, @Valid DemoFormObj demoForm) throws VanySoftBaseException {
        TDemoForm form = new TDemoForm();
        TDemoForm oldform = demoFormService.queryTDemoForm();
        form.setDemoformkey(demoForm.getDemoformkey());
        if (demoForm.isDelivery()) {
            form.setDelivery("1");
        } else {
            form.setDelivery("0");
        }
        form.setType(demoForm.getType());
        form.setName(demoForm.getName());
        if (demoForm.getDemoDate() != null && !"".equalsIgnoreCase(demoForm.getDemoDate())) {
            Date demoDate = DateUtil.string2Date(demoForm.getDemoDate(), "yyyy-MM-dd");
            form.setDemodate(demoDate);
        }
        form.setIndustry(demoForm.getIndustry());
        form.setRemark(demoForm.getRemark());
        form.setResource(demoForm.getResource());
        form.setUseraccount(userInfo.getUsername());
        form.setOprdate(new Date());


        //业务日志存储
        TSystemLog systemLog = new TSystemLog();
        String logid = UUID.randomUUID().toString().replaceAll("-", "");
        systemLog.setLogid(logid);
        systemLog.setUserid(userInfo.getId());
        systemLog.setUsername(userInfo.getChineseName());
        systemLog.setLogip(request.getRemoteAddr());
        systemLog.setOperationtime(new Date());
        systemLog.setOperationcontent("DEMO表单数据修改，新信息为：" + form.toString() + ";原信息为：" + oldform.toString());
        if (userInfo.getOrgs().size() > 0) {
            Org org = userInfo.getOrgs().get(0);
            systemLog.setOrgcode(org.getCode());
            systemLog.setOrgname(org.getOrgName());
        }
        systemLog.setOperationtype("DEMO_TABLE_UPDATE");
        systemLog.setOperationname("DEMO表单_修改");
        try {
            demoFormService.upateTDemoForm(form);
            systemLog.setStatus("SUCCESS");
            systemLogService.insertSystemLogs(systemLog);
        } catch (Exception e) {
            e.printStackTrace();
            systemLog.setStatus("FAIL");
            systemLogService.insertSystemLogs(systemLog);
        }
        return new JSONResponse();
    }

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public JSONResponse queryDemoForm(HttpServletRequest request) throws VanySoftBaseException {
        TDemoForm obj = demoFormService.queryTDemoForm();

        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, obj);
    }
}
